package com.jsyxdl.aspect;

import com.hustcad.plm.pdm.common.model.dto.search.ConditionGroupDTO;
import com.hustcad.plm.pdm.common.model.dto.search.SearchAttributeDTO;
import com.hustcad.plm.pdm.common.model.dto.search.SearchAttributeGroupDTO;
import com.hustcad.plm.pdm.common.model.dto.search.SearchConditionDTO;
import com.hustcad.plm.pdm.iba.model.vo.IbaDefVO;
import com.hustcad.plm.pdm.iba.service.TyplmIbaDefService;
import com.hustcad.plm.pdm.partbom.constant.PartBomPreferenceConstant;
import com.hustcad.plm.pdm.preference.service.TyPreferenceValueService;
import com.ty.basic.entity.CTyEntityBaseDO;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Aspect
@Component
public class QueryByConditionForPageAspect {
    @Resource
    TyplmIbaDefService typlmIbaDefService;
    @Resource
    private TyPreferenceValueService tyPreferenceValueService;

    @SneakyThrows
    @Before(value = "execution(* com.hustcad.plm.pdm.common.service.search.TyplmCommonSearchService.queryByConditionForPage(..))")
    public void queryByConditionForPage(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        SearchConditionDTO searchConditionDTO = (SearchConditionDTO) args[0];
        if(Objects.isNull(searchConditionDTO) || StringUtils.isBlank(searchConditionDTO.getKeyword())) {
            return;
        }
        // 首选项查询快速查询的IBA列
        Map<String, Object> preferenceValueMap = tyPreferenceValueService.getPreferenceValueMap(
                Collections.singletonList(PartBomPreferenceConstant.KEYWORD_RETRIEVAL_PROPERTIES));
        String keywordRetrievalProperties = (String) preferenceValueMap.getOrDefault(
                PartBomPreferenceConstant.KEYWORD_RETRIEVAL_PROPERTIES, "");
        if (StringUtils.isNotBlank(keywordRetrievalProperties)) {
            ConditionGroupDTO conditionGroup = new ConditionGroupDTO();
            SearchAttributeGroupDTO customCondition = new SearchAttributeGroupDTO();

            List<IbaDefVO> ibaDefVOS = typlmIbaDefService.batchQueryIbaDefByName(
                    Arrays.asList(keywordRetrievalProperties.split(",")));
            Map<String, IbaDefVO> nameIbaMap = ibaDefVOS.stream().collect(
                    Collectors.toMap(CTyEntityBaseDO::getName, item -> item, (k1, k2) -> k1));

            Map<String, String> keyValueMap = new HashMap<>();
            keyValueMap.put("number", "*"+searchConditionDTO.getKeyword()+"*");
            keyValueMap.put("name", "*"+searchConditionDTO.getKeyword()+"*");
            for (String s : keywordRetrievalProperties.split(",")) {
                keyValueMap.put(s, "*"+ searchConditionDTO.getKeyword() + "*");
            }
            SearchAttributeGroupDTO conditionGroupDTO = new SearchAttributeGroupDTO();
            conditionGroupDTO.setConditionJoint("or");
            List<SearchAttributeDTO> searchAttributeDTOS = new ArrayList<>();
            keyValueMap.forEach((key, value) -> {
                SearchAttributeDTO searchAttributeDTO = new SearchAttributeDTO();
                if (nameIbaMap.containsKey(key)) {
                    searchAttributeDTO.setIbaDefOid(nameIbaMap.get(key).getOid());
                    searchAttributeDTO.setIbaDefOtype(nameIbaMap.get(key).getOtype());
                }
                searchAttributeDTO.setPropertyValue(value);
                searchAttributeDTO.setPropertyType("String");
                searchAttributeDTO.setSymbol("=");
                searchAttributeDTO.setPropertyName(key);
                searchAttributeDTO.setTypeList(searchConditionDTO.getTypeList());
                searchAttributeDTOS.add(searchAttributeDTO);
            });
            customCondition.setConditionJoint("or");
            customCondition.setConditions(searchAttributeDTOS);
            conditionGroup.setConditionJoint("and");
            conditionGroup.setConditionGroups(Collections.singletonList(customCondition));
            if (CollectionUtils.isEmpty(searchConditionDTO.getCustomConditionList())) {
                List<ConditionGroupDTO> conditionGroupDTOS = new ArrayList<>();
                conditionGroupDTOS.add(conditionGroup);
                searchConditionDTO.setCustomConditionList(conditionGroupDTOS);
            } else {
                List<ConditionGroupDTO> conditionGroups = searchConditionDTO.getCustomConditionList();
                conditionGroups.add(conditionGroup);
            }
            searchConditionDTO.setKeyword("");
        }
    }
}
